import os
import math
import shutil

# 定义源目录和目标目录
source_dir = r"\\192.168.1.137\share\Test\livan\TongueConstitutionTrain_DenseNet121_V2\\validation"
target_dir = r"\\192.168.1.137\share\Test\livan\TongueConstitutionTrain_DenseNet121_V2\\test"

def move_third_files(source, target):
    # 遍历源目录中的每个文件夹
    for root, dirs, files in os.walk(source):
        # 计算需要复制的文件数量
        total_files = len(files)
        num_files_to_move = math.floor(total_files / 2)
        if total_files >= 1 and num_files_to_move == 0:
            num_files_to_move = 1

        # 获取相对路径，保持目录结构
        relative_path = os.path.relpath(root, source)
        target_folder = os.path.join(target, relative_path)

        # 确保目标文件夹存在
        os.makedirs(target_folder, exist_ok=True)

        # 移动文件
        for file in files[:num_files_to_move]:
            source_file = os.path.join(root, file)
            target_file = os.path.join(target_folder, file)
            shutil.copy(source_file, target_file)

        print(f"已复制 {num_files_to_move} 个文件从 {root} 到 {target_folder}")

if __name__ == "__main__":
    move_third_files(source_dir, target_dir)
    print("文件复制完成！")